/****************************************************
 * 													*
 * 					BanzaiP2P						*
 * 													*
 * Versione: 0.1.4									*
 * Autore: D'Annessa Marco							*
 * File: file_list_utility.c						*
 ****************************************************/


#include "file_list_utility.h"



struct token* make_token(char *q){

	
	struct token *result,*temp;
	temp = result = (struct token*)malloc(sizeof(struct token));
	
	char *token;
	char *set=" ";
	char *str;
	str = (char*)malloc(strlen(q)+1);
	strcpy(str,q);

	token = strtok(str,set);
	
	do{	
		if(strlen(token)>1){
			//temp->tok = (char*)malloc(strlen(token));
			int b=strlen(token);	int i;
			if(token[b-4]=='.'){
				temp->tok = (char*)malloc(strlen(token)-4);
				for(i=0;i<b-4;i++)
					temp->tok[i]=token[i];
			}
			else{
				temp->tok = (char*)malloc(strlen(token));
				strcpy(temp->tok,token);
			}
			
			
			
			temp->next = (struct token*)malloc(sizeof(struct token));
			temp = temp->next;
				
		}
		
	token = strtok(NULL,set);
		
	}while(token!=NULL);
	temp->next=NULL;
	
	return result;
};



struct file_list * create_list(char * dir_path){


	DIR *directory;
	struct dirent *info_directory;
	struct file_list *list, *temp;
	struct stat *file_info;

	char str_temp[500];
	char type_temp[3];
	int i;
	int id=0;

	list = temp = (struct file_list*)malloc(sizeof(struct file_list));
	file_info = (struct stat*)malloc(sizeof(struct stat));
	info_directory = (struct dirent*)malloc(sizeof(struct dirent));

	directory =opendir(dir_path);

	if(directory==NULL)
		printf("directory non trovata");

	else{
		info_directory =readdir(directory);

		while(info_directory!=NULL){
			
			if(strcmp(info_directory->d_name,".")!=0 && strcmp(info_directory->d_name,"..")!=0){

				temp->next = (struct file_list*)malloc(sizeof(struct file_list));	
				temp->file = (struct file*)malloc(sizeof(struct file));
				temp->file->name = (char*)malloc(strlen(info_directory->d_name));
				strcpy(temp->file->name,info_directory->d_name);

				temp->token_file_name = (struct token*)malloc(sizeof(struct token));
				temp->token_file_name = make_token(info_directory->d_name);

				strcat(str_temp,dir_path);
				strcat(str_temp,"/");
				temp->file->path = (char*)malloc(strlen(str_temp));
				strcpy(temp->file->path,str_temp);		//path
				
				strcat(str_temp,info_directory->d_name);
				
				stat(str_temp,file_info);
			
				temp->file->size = file_info->st_size;		//size
				temp->file->last_mod = file_info->st_mtime;	//last modify

				strcpy(str_temp,"");
				
				i=strlen(info_directory->d_name);
				type_temp[2]=info_directory->d_name[i-1];
				type_temp[1]=info_directory->d_name[i-2];				
				type_temp[0]=info_directory->d_name[i-3];
				strcpy(temp->file->type,type_temp);
				//strcpy(temp->file->type,info_directory->d_type);
				temp->file->ID=++id;

				temp = temp->next;
				
			}

			info_directory =readdir(directory);
		}
	}
	
	free(info_directory);
	free(file_info);
	closedir(directory);
	
	return list;

};



int str_matching(char* str_f,char *str_q){

	int val =0;
	int i=0;
	if(strlen(str_q)==strlen(str_f)){
		while(i<strlen(str_q) && i<strlen(str_f)){
	
			if( (str_f[i]>95) && ( (str_q[i]==str_f[i]) || (str_q[i]==(str_f[i]-32)) ) ){
				i++;	
				val=1;
			}
			else{
				if( (str_f[i]<91) && ( (str_q[i]==str_f[i]) || (str_q[i]==(str_f[i]+32)) ) ){
					i++;
					val=1;
				}
	
				else{
					val=0;
					break;
				}
			}
		}
	}

	return val;
};



struct file_list* search_file(struct file_list *list, struct token *query){
	
	struct file_list *temp,*result,*t_list;

	result = temp = (struct file_list*)malloc(sizeof(struct file_list));
	temp->next = NULL;
	t_list = (struct file_list*)malloc(sizeof(struct file_list));
	struct token *tquery;	
	tquery = (struct token*)malloc(sizeof(struct token));
	int stat=0,res;
	
	printf("\nsearch\n");
	while(list->next!=NULL){
		t_list->token_file_name=list->token_file_name;
		while(list->token_file_name->next!=NULL){
			tquery=query;
			while(tquery->next!=NULL){
			
				// str_matching(list->token_file_name->tok, tquery->tok)==1
				if(strcasecmp(list->token_file_name->tok, tquery->tok)==0){	
					temp->file = (struct file*)malloc(sizeof(struct file));
					
					temp->file->name=(char*)malloc(strlen(list->file->name));
					strcpy(temp->file->name,list->file->name);
					strcpy(temp->file->type,list->file->type);
					temp->file->size=list->file->size;
					//altri campi
					
					
					temp->next = (struct file_list*)malloc(sizeof(struct file_list));
					temp=temp->next;
					temp->next=NULL;
					stat=1;
					break;
				}
				else{
					tquery=tquery->next;	}	
		
			}
			
			if(stat==1){
				stat=0;
				break;}
			else{
				list->token_file_name= list->token_file_name->next;}
			
		}
		list->token_file_name=t_list->token_file_name;
		
		list =list->next;
	}
	//free(temp);
	free(tquery);
	return result;
	
	
};





/*
 * 
 

	struct file_list * f,*r,*ff;
	ff=f= create_list("directory");

	while(f->next!=NULL){
		printf("name: %s\n",f->file->name);
		f=f->next;}

	struct token * qt;
	qt = make_token("U2");
	/*
	while(qt->next!=NULL){
		printf("name: %s\n",qt->tok);

		qt=qt->next;}
	*/

	//stamparisultati
	/*r = search_file(ff,qt);
	printf("\n\n");

	while(r->next!=NULL){
		printf("name!!: %s\n",r->file->name);
		printf("path: %s\n",r->file->path);
		printf("size: %d bytes\n",r->file->size);
		printf("ultima modifica: %ld\n",(r->file->last_mod/3600));
		printf("ID: %d\n",r->file->ID);
		printf("type: %s\n\n",r->file->type);

		r=r->next;
	}
*/